﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web.Security;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Tesla.Service.DataContracts;
using Microsoft.Practices.EnterpriseLibrary.ExceptionHandling;
using Microsoft.Practices.EnterpriseLibrary.Logging;
using System.ServiceModel;
using Tesla.Service.WcfServiceCommon;
//using Telerik.Web.UI;
using Tesla.Util.Utilities;
using System.IO;

namespace Tesla.Web.Pages
{
    public partial class EmployeeDetails : PageBase
    {
        #region Class Level Variables
        private EmployeeServiceDispatcher employeeDispatcher = null;
        private int? EmployeeRecordCount = 0;
        #endregion


        protected void Page_Load(object sender, EventArgs e)
        {
            List<EmployeeDC> Employee = null;

            employeeDispatcher = new EmployeeServiceDispatcher();

            try
            {
                if (!IsPostBack)
                {
                    if (Request.QueryString["ID"] != null)
                    {
                        Employee = employeeDispatcher.ReadEmployee(Convert.ToInt32(Request.QueryString["ID"])).Cast<EmployeeDC>().ToList();
                        SetValuesForControls(Employee);
                        Legend1.InnerText = "Update Employee";
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                int? EmployeeID = 0, success = 0;
                EmployeeDC createdEmployee = new EmployeeDC();
                createdEmployee.EmployeeFirstName = txtEmployeeFirstName.Text;
                createdEmployee.EmployeeLastName = txtEmployeeLastName.Text;
                createdEmployee.EmployeeMiddleName = txtEmployeeMiddleName.Text;
                createdEmployee.EmployeePANNo = txtEmployeePANNo.Text;
                createdEmployee.EmployeeContactNo = Convert.ToInt32(txtEmployeeContactNo.Text);
                createdEmployee.EmployeeEmailID = txtEmployeeEmailID.Text;
                createdEmployee.EmployeeRelevantExp = Convert.ToInt32(txtEmployeeRelevantExp.Text);

                createdEmployee.EmployeePicture = imgUpload.PostedFile.FileName;

                //createdEmployee.EmployeePicture = new byte[imgUpload.PostedFile.ContentLength];
                //imgUpload.PostedFile.InputStream.Read(createdEmployee.EmployeePicture, 0, imgUpload.PostedFile.ContentLength);

                //createdEmployee.EmployeeWorkdayDuration = Convert.ToInt32(txtWorkdayDuration.Text);

                employeeDispatcher = new EmployeeServiceDispatcher();

                if (ViewState["SelectedEmployeeId"] == null)
                {
                    EmployeeID = employeeDispatcher.CreateEmployee(createdEmployee);
                }
                else
                {
                    createdEmployee.EmployeeID = Convert.ToInt32(ViewState["SelectedEmployeeId"]);
                    success = employeeDispatcher.UpdateEmployee(createdEmployee);
                }
                if (!Page.ClientScript.IsClientScriptBlockRegistered("saveAlert"))
                {
                    Page.ClientScript.RegisterClientScriptBlock(typeof(Page), "saveAlert", "alert('Data has been succesfully saved '); window.location.href('EmployeeSearch.aspx')", true);
                }


            }

            catch (Exception ex)
            {
                throw ex;
            }
        }

        private void SetValuesForControls(List<EmployeeDC> Employee)
        {
            ViewState["SelectedEmployeeId"] = ((EmployeeDC)Employee[0]).EmployeeID;
            this.txtEmployeeFirstName.Text = ((EmployeeDC)Employee[0]).EmployeeFirstName;
            this.txtEmployeeLastName.Text = ((EmployeeDC)Employee[0]).EmployeeLastName;
            this.txtEmployeeMiddleName.Text = ((EmployeeDC)Employee[0]).EmployeeMiddleName;
            this.txtEmployeePANNo.Text = ((EmployeeDC)Employee[0]).EmployeePANNo;
            this.txtEmployeeContactNo.Text = ((EmployeeDC)Employee[0]).EmployeeContactNo.ToString();
            this.txtEmployeeEmailID.Text = ((EmployeeDC)Employee[0]).EmployeeEmailID;
            this.txtEmployeeRelevantExp.Text = ((EmployeeDC)Employee[0]).EmployeeRelevantExp.ToString();

            if(!string.IsNullOrEmpty(((EmployeeDC)Employee[0]).EmployeePicture))
                this.imgEmployee.ImageUrl = "~/Images/" + ((EmployeeDC)Employee[0]).EmployeePicture;
            //this.imgEmployee = new System.Drawing.Image.FromStream(new MemoryStream(((EmployeeDC)Employee[0]).EmployeePicture));

            //if (((EmployeeDC)Employee[0]).EmployeePicture != null && ((EmployeeDC)Employee[0]).EmployeePicture.Length > 0)
            //{
            //    MemoryStream bytImg = new MemoryStream(((EmployeeDC)Employee[0]).EmployeePicture);
            //    System.Drawing.Image returnImage = System.Drawing.Image.FromStream(bytImg, true);
            //    //this.imgEmployee = returnImage;
            //}
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            try
            {
                Response.Redirect(VirtualPathUtility.ToAbsolute("~/") + @"Pages/Administrator/EmployeeSearch.aspx", false);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }
}
